class ProjectsController < ApplicationController
  before_filter :isCompanyOwner, :expect => []
  before_filter :isNotCompanyOwner, :only => []
  #private
  # GET /projects
  # GET /projects.json
  def index
    @projects = Project.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @projects }
    end
  end

  # GET /projects/1
  # GET /projects/1.json
  def show
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @project }
    end
  end
  
  def sprintPoints(projid)
    sprint_backlogs = SprintBacklog.where("project_pid" => projid )
    this=sprint_backlogs.first
    last=sprint_backlogs.first
    sprint_backlogs.each do |sb|
      last=this
      this=sb
    end
    points = 0
    user_stories = UserStory.where('sprint_backlog_id' => last.id)
    user_stories.each do |us|
       usr = UserStoryRating.find_by_id(us.user_story_rating_id)
       done_id = UserStoryState.find_by_status("Abgenommen").id 
       points += usr.rating if(us.user_story_state_pid == done_id)
    end
    return  points
  end
  
  def showall
    @cid = current_user.company_pid   
    @projs = Project.joins(:company).where('company_id' => @cid)
    @data = Array.new

    # Alles in ein Array dübeln, damit das nicht im View gemacht werden muss
    @projs.each do |proj|
      @pa = ProjectData.new
      @pa.name = proj.name
      @pa.id = proj.id
      @pa.sprintpunkte = sprintPoints(proj.id)
      @pa.sprint_id = SprintBacklog.where("project_pid" => proj.id).count
      @users = ProjectToUser.joins(:user).where('project_id' => proj.id).count
      @pa.users = @users
      @pa.speed = sprintspeed(proj.id)
      
      @back = ProductBacklog.where(:project_pid => proj.id).first
      if @back.nil?
        @pa.all_stories = 0
        @pa.open_stories = 0
      else 
        @stories = UserStory.where(:product_backlog_pid => @back)
        @pa.all_stories = @stories.count
        @state_id = UserStoryState.where(:status => "Offen").first.id
        @pa.open_stories = @stories.where(:user_story_state_pid => @state_id ).count
      end
      @data.push(@pa)
    end   
   
    render :layout => nil  
  end
  
  def new
    @project = Project.new

    render :layout => nil
  end

  def edit
    @id = params[:id].split("_")[2]
    @proj = Project.new
    @proj = Project.find(@id)
  
    @spl = @proj.sprint_length
    @spnr = SprintBacklog.where("project_pid" => @id ).count
    @dev_id = Rolename.find_by_name("Developer")
    @devs = ProjectToUser.where('project_id' => @id, "rolename_id" => @dev_id).count
    @owners_id = Rolename.find_by_name("Product Owner")
    @owners = ProjectToUser.where('project_id' => @id, "rolename_id" => @owners_id).count
    @masters_id  = Rolename.find_by_name("Scrummaster")
    @masters  = ProjectToUser.where('project_id' => @id, "rolename_id" => @masters_id).count
  
    @name = @proj.name  
    @sprint = @proj.sprint_id
    
    @project_points=0
    
    @back = ProductBacklog.where(:project_pid => @id).first
    if @back.nil?
       @all = 0
       @open = 0
    else 
       @all = UserStory.where(:product_backlog_pid => @back)
       @us_count = @all.count
       @state_id = UserStoryState.where(:status => "Offen").first.id
       @open = @all.where(:user_story_state_pid => @state_id ).count
       @state_id = UserStoryState.where(:status => "Entwurf").first.id
       @dropped = @all.where(:user_story_state_pid => @state_id ).count
       #@state_id = UserStoryState.where(:status => "Im&nbsp;Sprintbacklog").first.id
       #@in_backlog = @all.where(:user_story_state_pid => @state_id ).count
       @state_id = UserStoryState.where(:status => "Zu&nbsp;pr&uuml;fen").first.id
       @ongoing = @all.where(:user_story_state_pid => @state_id ).count
       #@state_id = UserStoryState.where(:status => "Zu bewerten").first.id
       #@rating = @all.where(:user_story_state_pid => @state_id ).count
       @state_id = UserStoryState.where(:status => "Abgenommen").first.id
       @done = @all.where(:user_story_state_pid => @state_id ).count
       @project_points = allpoints
       #@state_id = UserStoryState.where(:status => "Neu bewerten").first.id
       #@new_rating = @all.where(:user_story_state_pid => @state_id ).count       
    end
    
   render :layout => nil
  end

  def create
    name = params[:items].split(",")[0]
    spl = params[:items].split(",")[1]  
    @project = Project.new
    @project.name = name
    @project.sprint_length = spl
    @project.sprint_id = 1
    @project.max_user_stories = 100
    @project.company_id = current_user.company_pid 
    
    if @project.save
      @pb = ProductBacklog.new
      @pb.project_pid = @project.id
      @sb = SprintBacklog.new
      @sb.sprint_start = DateTime.now.to_date
      @sb.sprint_end = DateTime.now.to_date + (spl.to_i*7)
      @sb.project_pid = @project.id
    end
    
    if @sb.save
      @project.sprint_id = @sb.id   
      if @project.save
        if @pb.save    
          render :text => @project.id
        else
          render :text => 0
        end
      else
        render :text => 0
      end
    else
      render :text => 0          
    end
  end

  def update
    id = params[:items].split(",")[0]
    @project = Project.find(id)
    stories = params[:items].split(",")[1]
    @project.max_user_stories = stories
    
    if @project.save
      render :text => 0
    else
      render :text => -1
    end
  end

  def destroy
    @project = Project.find(params[:items])
    @pid = @project.id
    @assigned_product_backlog = ProductBacklog.where("project_pid" => @pid )
    @bid = @assigned_product_backlog.first.id
    @assigned_stories = UserStory.where("product_backlog_pid" => @bid )
    @assigned_sprint_backlogs = SprintBacklog.where("project_pid" => @pid )
    
    @assigned_users = User.where("project_id" => @pid )
    
    #user referenzen auflösen
    @assigned_users.each do |us|
      us.project_id = nil
    end
    
    #sprint backlogs auflösen 
    @assigned_sprint_backlogs.each do |sb|
      sb.destroy
    end
    @assigned_product_backlog.first.destroy
    @project.destroy
    
    render :text => 0     
  end
  
  def sprintspeed(pid)
    sprint_backlog_ids = SprintBacklog.where("project_pid" => pid ).select(:id)
    points = 0
    sprint_backlog_ids.each do |value|
      user_stories = UserStory.where('sprint_backlog_id' => value.id)
      user_stories.each do |us|
         usr = UserStoryRating.find_by_id(us.user_story_rating_id)
         done_id = UserStoryState.find_by_status("Abgenommen").id 
         points += usr.rating if(us.user_story_state_pid == done_id)
      end
    end
    return  points / sprint_backlog_ids.count 
  end
  
  def allpoints
    points = 0
    user_stories = UserStory.where('product_backlog_pid' => @back.id)
    user_stories.each do |us|
       usr = UserStoryRating.find_by_id(us.user_story_rating_id)
       done_id = UserStoryState.find_by_status("Abgenommen").id 
       points += usr.rating if(us.user_story_state_pid == done_id)
    end
    return  points
  end
end

class ProjectData
  attr_accessor :name, :users, :id, :all_stories, :open_stories, :sprint_id, :speed, :sprintpunkte
end
